package gee

import (
	"fmt"
	"log"
	"net/http"
	"runtime"
	"strings"
	"time"
)

//打印堆栈错误信息
func trace(message string)string{
	var pcs [32]uintptr
	n:=runtime.Callers(3,pcs[:])

	var str strings.Builder
	str.WriteString(message+"\nTraceback:")
	for _,pc:=range pcs[:n]{
		fn:=runtime.FuncForPC(pc)
		file,line:=fn.FileLine(pc)
		str.WriteString(fmt.Sprintf("\n\t%s:%d",file,line))
	}
	return str.String()
}

//捕捉错误中间件
func Recovery()HandlerFunc{
	return func(c *Context) {
		defer func() {
			if err:=recover();err!=nil{
				message:=fmt.Sprintf("%s",err)
				log.Printf("%s\n\n",trace(message))
				c.Fail(http.StatusInternalServerError,"Internal Server Error")
			}
		}()
		c.Next()
	}
}

func Logger() HandlerFunc {
	return func(c *Context) {
		// Start timer
		t := time.Now()
		// Process request
		c.Next()
		// Calculate resolution time
		log.Printf("[%d] %s in %v", c.StatusCode, c.Req.RequestURI, time.Since(t))
	}
}
